package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EntityRent;
import com.ruoyi.system.service.IEntityRentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 *  出库Controller
 * 
 * @author ruoyi
 * @date 2020-12-27
 */
@Controller
@RequestMapping("/system/rent")
public class EntityRentController extends BaseController
{
    private String prefix = "system/rent";

    @Autowired
    private IEntityRentService entityRentService;

    @RequiresPermissions("system:rent:view")
    @GetMapping()
    public String rent()
    {
        return prefix + "/rent";
    }

    /**
     * 查询出库单列表
     */
    @RequiresPermissions("system:rent:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EntityRent entityRent)
    {
        startPage();
        List<EntityRent> list = entityRentService.selectEntityRentList(entityRent);
        return getDataTable(list);
    }

    /**
     * 导出出库单列表
     */
    @RequiresPermissions("system:rent:export")
    @Log(title = "导出出库单列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EntityRent entityRent)
    {
        List<EntityRent> list = entityRentService.selectEntityRentList(entityRent);
        ExcelUtil<EntityRent> util = new ExcelUtil<EntityRent>(EntityRent.class);
        return util.exportExcel(list, "rent");
    }

    /**
     * 新增出库单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存出库单
     */
    @RequiresPermissions("system:rent:add")
    @Log(title = "新增保存出库单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(EntityRent entityRent) {
        return entityRentService.insertEntityRent(entityRent);
    }

    /**
     * 修改出库单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        EntityRent entityRent = entityRentService.selectEntityRentById(id);
        mmap.put("entityRent", entityRent);
        return prefix + "/edit";
    }
    /**
     * 打印出库单详情
     */
    @GetMapping("/gotoPrintRentInfo/{id}")
    public String getRentInfo(@PathVariable("id") Long rentId, ModelMap mmap) {
        EntityRent entityRent = new EntityRent();
        entityRent.setId(rentId);
        mmap.put("entityRent", entityRent);
        return prefix + "/invoice";
    }

    @PostMapping("/getRentInfo")
    @ResponseBody
    public AjaxResult getRentInfo(Long id) {
        return AjaxResult.success(entityRentService.getRentInfo(id));
    }
    /**
     * 修改保存出库单
     */
    @RequiresPermissions("system:rent:edit")
    @Log(title = "修改保存出库单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editSave(EntityRent entityRent) {
        return entityRentService.updateEntityRent(entityRent);
    }

    /**
     * 删除出库单
     */
    @RequiresPermissions("system:rent:remove")
    @Log(title = "删除出库单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(entityRentService.deleteEntityRentByIds(ids));
    }

    /**
     * 总出库单数量
     * @return
     */
    @GetMapping("/getTotalRentCount")
    @ResponseBody
    public AjaxResult getTotalRentCount(){
        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
        ajax.put("value", entityRentService.getTotalRentCount());
        return ajax;
    }
    /**
     * 每年出库单统计
     * @return
     */
    @GetMapping("/queryEveryYearRentCount")
    @ResponseBody
    public AjaxResult queryEveryYearRentCount(){
        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
        ajax.put("value", entityRentService.queryEveryYearRentCount());
        return ajax;
    }
    /**
     * 过去12个月出库单统计
     * @return
     */
    @GetMapping("/queryLastEveryMonthRentCount")
    @ResponseBody
    public AjaxResult queryLastEveryMonthRentCount(){
        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
        ajax.put("value", entityRentService.queryLastEveryMonthRentCount());
        return ajax;
    }
    /**
     * 过出库单最多的十个客户
     * @return
     */
    @GetMapping("/queryTopTenCustomer")
    @ResponseBody
    public AjaxResult queryTopTenCustomer(){
        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
        ajax.put("value", entityRentService.queryTopTenCustomer());
        return ajax;
    }

}
